package com.sljy.hzk.ui;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.ImageView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.sljy.hzk.R;
import com.sljy.hzk.annotation.ActivityInfo;
import com.sljy.hzk.base.BaseActivity;
import com.sljy.hzk.ui.exam.ExamDoActivity;
import com.sljy.hzk.util.picture.TakePictureUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import butterknife.BindView;

@ActivityInfo(layout = R.layout.activity_pic_cut, isShowActionBar = true, title = "修改头像")
public class PicHeadCutActivity extends BaseActivity {

    @BindView(R.id.img)
    ImageView img;
    boolean isCut = false;

    @Override
    protected void init() {
//        if(isCut){
//            Glide.with(PicHeadCutActivity.this).asBitmap().load(data.getStringExtra(MediaStore.EXTRA_OUTPUT)).into(new SimpleTarget<Bitmap>() {
//                @Override
//                public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
//                    Bitmap bit = getCircleBitmap(resource);
//                    saveBitmap(bit);
//                }
//
//            }); //方法中设置asBi
//            return;
//        }

    }

    /*** @description 获取圆形裁剪框内截图
 * @param bitmap src图片
 * @return
         */
    public static Bitmap getCircleBitmap(Bitmap bitmap) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        paint.setDither(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        //在画布上绘制一个圆 -1是为了去掉白色的边框
        canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
                bitmap.getWidth() / 2 - 1, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        Log.i("aaaa", "getCircleBitmap()     output=" + output);
        return output;
    }

    /**
     * @return void
     * @Title: saveMyBitmap
     * @Description: 保存bitmap对象到裁剪后的文件中
     */
    public static void saveMyBitmap(File file, Bitmap mBitmap) {
        try {
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            if (!file.exists()) {
                file.createNewFile();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        FileOutputStream fOut = null;
        try {
            fOut = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        try {
            fOut.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            fOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 保存图片
     * 首先获取裁剪框里的图片
     * 然后保存到裁剪文件croppedImagePath中
     */
    private void saveBitmap(Bitmap bitmap) {
        File file = null;
        if (bitmap != null) {
            // 取出裁剪图片
            Bitmap clipBitmap = bitmap;
            Log.i(TAG, "saveBitmap()  clipBitmap=" + clipBitmap);
            file = new File(this.getExternalFilesDir("cache").getAbsolutePath());
            saveMyBitmap(file, clipBitmap);
        }
        if(file == null) return;
        Glide.with(PicHeadCutActivity.this).load(file).into(img);
       // finish();
    }


}
